package com.margo.project.web.session.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import io.swagger.annotations.Api;

/**
 * http://localhost:8080/session/login?girl=margo
 * http://localhost:8081/session/check
 * @author fxb208
 *
 */
@Api(tags={"Spring-Session"})
@RequestMapping("/session")
@Controller
public class MargoSessionController {
	
	@Value("${server.port}")
	private String port;
	
	Logger logger = LoggerFactory.getLogger(MargoSessionController.class);
	
	//同时支持post方法和get方法
    @RequestMapping(value="/login",method= {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public String login(String girl , WebRequest request){
        request.setAttribute("girl" , girl , WebRequest.SCOPE_SESSION);
        logger.info(girl);
        return "port:" + port +",login success";
    }
    
    @RequestMapping(value="/check",method= {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public String checkUser(WebRequest request){
        String girl = (String)request.getAttribute("girl", WebRequest.SCOPE_SESSION);
        logger.info(girl);
        if(girl != null){
            return "port:" + port + ",girl=" + girl;
        }else{
            return "port:" + port + ", redirect to login!";
        }
    }

}
